package com.tlias.controller;

import com.tlias.Service.DeptService;
import com.tlias.pojo.Dept;
import com.tlias.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    //private static Logger log = (Logger) LoggerFactory.getLogger(DeptController.class);
    //注入Service对象
    @Autowired
    private DeptService deptService;

    /*
    * 查寻部门数据
    * */
    //@RequestMapping(value = "/depts",method = RequestMethod.GET) //指定请求方式为GET
    @GetMapping
    public Result list(){
        log.info("查询全部部门数据");

        //调用service查询部门数据
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    /*
    * 删除部门数据
    * */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) throws Exception {
        log.info("根据ID删除部门：{}",id);
        //调用service删除部门
        deptService.deleteByid(id);
        return Result.success();
    }

    /*
    * 新增部门
    * */
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门：{}",dept);
        //调用service新增部门
        deptService.add(dept);
        return Result.success();
    }

    /*
    * 根据ID查询数据
    * */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询数据：{}",id);
        //调用service查询数据
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    /*
    * 修改部门数据
    * */
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("更改部门：{}",dept);
        //调用service更改数据
        deptService.update(dept);
        return Result.success();
    }
}
